﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Computer_Manager.BUS;
using Computer_Manager.DTO;
using DevExpress.XtraReports.UI;

namespace Computer_Manager.GUI
{
    public partial class frmViewOrder : DevExpress.XtraEditors.XtraForm
    {
        public frmViewOrder()
        {
            InitializeComponent();
        }
        DTO_order dto_order = null;
        BUS_order bus_order = new BUS_order();

        DTO_order_detail dto_order_detail =null;
        BUS_order_detail bus_order_detail = new BUS_order_detail();
        DataTable data_order = null;

        public int id_order
        {
            get;
            set;
        }
        private void frmViewOrder_Load(object sender, EventArgs e)
        {
            dto_order = new DTO_order();
            dto_order.id_order = id_order;
            data_order = bus_order.GetByID(dto_order);
            if (bool.Parse(data_order.Rows[0]["type"].ToString()))
            {
                rdgTypeOrder.SelectedIndex = 1;
            }
            txtSoHoaDon.Text = data_order.Rows[0]["codes_order"].ToString();
            txtTenKH.Text = data_order.Rows[0]["fullname"].ToString();
            txtAddress.Text = data_order.Rows[0]["address"].ToString();
            txtEmail.Text = data_order.Rows[0]["email"].ToString();
            txtPhone.Text = data_order.Rows[0]["phone"].ToString();
            dto_order_detail = new DTO_order_detail();
            dto_order_detail.id_order = id_order;
            var listOrder = bus_order_detail.GetByOrderID(dto_order_detail);
            grcOrderList.DataSource = listOrder;

            lblTongTien.Text = string.Format("{0:0,0} vnđ", getAllMoney(listOrder));


        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private float getAllMoney(DataTable orderlist)
        {
            float Money = 0;
            for (int i = 0; i < orderlist.Rows.Count; i++)
            {
                try
                {
                    Money += float.Parse(orderlist.Rows[i]["money"].ToString());
                }
                catch (Exception) { }
            }
            return Money;
        }
        private void btnExportOrder_Click(object sender, EventArgs e)
        {
            //grvOrderList.ShowPrintPreview();
            //DataSet dsOrder = new DataSet();
            //dsOrder.Tables.Add(data_order);
            DataSet dsOrder = new DataSet();
            DataTable dtBill = new DataTable();
            dtBill.Columns.Add("codes_pro", typeof(string));
            dtBill.Columns.Add("name_pro", typeof(string));
            dtBill.Columns.Add("price_out", typeof(string));
            dtBill.Columns.Add("amount", typeof(string));
            dtBill.Columns.Add("money", typeof(string));
            
            //DataView dv = (DataView)(gvBill.DataSource);
            //dtBill = dv.ToTable();
            DataRow dr;
            for (int i = 0; i < grvOrderList.RowCount; i++)
            {
                dr = dtBill.NewRow();
                dr = (DataRow)grvOrderList.GetDataRow(i);
                dtBill.ImportRow(dr);
            }
            dsOrder.Tables.Add(dtBill);

            XtraReportOrder rptOrder = new XtraReportOrder();

            rptOrder.DataSource = dsOrder;
            rptOrder.DataMember = dsOrder.Tables[0].TableName;

            rptOrder.lblFullName.Text = txtTenKH.Text;
            rptOrder.lblCodesOrder.Text = txtSoHoaDon.Text;
            rptOrder.lblAddress.Text = txtAddress.Text;
            rptOrder.lblEmail.Text = txtEmail.Text;
            rptOrder.lblPhone.Text = txtPhone.Text;
            rptOrder.lblPrintDate.Text = DateTime.Now.ToString();
            rptOrder.lblCodesPro.DataBindings.Add("Text", null, "codes_pro");
            rptOrder.lblNamePro.DataBindings.Add("Text", null, "name_pro");
            rptOrder.lblPrice.DataBindings.Add("Text", null, "price_out");
            rptOrder.lblAmount.DataBindings.Add("Text", null, "amount");
            rptOrder.lblMoney.DataBindings.Add("Text", null, "money");
            rptOrder.PrintDialog();
        }
    }
}
